/*
 * @Author: yuqy
 * @Date: 2023-09-26 18:13:33
 * @LastEditors: yuqy
 * @LastEditTime: 2023-10-23 13:55:20
 * @Descripttion: -
 */
import { getAction } from "@/api";
import { mapGetters } from 'vuex';
import { Message } from "view-ui-plus";
// 声网集成的黑板
import WhiteBoardWrap from "@/components/whiteboard.vue";
// 自定义的白板
// import WhiteBoardSelf from "@/components/whiteboardSelf.vue";
// 用于页面间通信
const userMsgBc = new BroadcastChannel('getUserMainMsg');
const minix = {
    data() {
        return {
            appIdentifier: null,
            uid: 0,
            uuid: null,
            roomToken: null
        }
    },
    components: {
        WhiteBoardWrap
    },
    computed: {
        ...mapGetters({
            uid_: 'user/getUserUid',
            nickName: 'user/getUserNickName'
        })
    },
    created() { },
    methods: {
        /**
         * @name: 创建互动白板教室
         * @msg:
         * @return {*}
         */
        async createRoom() {
            let { uuid, appIdentifier, roomToken } = await getAction(
                "/whiteboardclient/fastBoard/gainToken",
                { code: this.code }
            );
            this.uuid = uuid;
            this.appIdentifier = appIdentifier;
            this.roomToken = roomToken;
            // 通知主讲页面  已经打开了白板
            userMsgBc.postMessage({ type: 'boardOpen' });
        },
        /**
         * @name: 听讲互动端，只需要调用加入房间即可
         * @msg:
         * @return {*}
         */
        async joinRoom() {
            let { uuid, appIdentifier, roomToken } = await getAction(
                "/whiteboardclient/fastBoard/joinRoom",
                { code: this.code }
            );
            this.uuid = uuid;
            this.appIdentifier = appIdentifier;
            this.roomToken = roomToken;
        },
        /**
         * @name: 用户操作
         * @msg: 
         * @param {*} type
         * @return {*}
         */
        closeFn(type) {
            // 给侧边栏按钮发送最小化信息
            if (type == 1) {
                userMsgBc.postMessage({ type: 'isMini', name: this.$route.name })
            }
            // 需要通知主讲页面，互动白板已经关闭了
            if (type == 2) {
                this.isSelfClose = true;
                userMsgBc.postMessage({ type: 'boardClose' })
                setTimeout(() => {
                    // 标记主动关闭白板
                    localStorage.isSelfClose = true
                    window.close()
                }, 100)
            }
        },
        /**
         * @name: 讲师禁止允许书写
         * @msg: 
         * @return {*}
         */        
        async isNoWrite() {
            this.switchLoading = true
            let val = !this.isAllWrite
            setTimeout(() => {
                userMsgBc.postMessage({ type: 'allBoardUsing', val });
                this.switchLoading = false;
                this.isAllWrite = val;
                let desc = val ? '已允许所有听讲书写' : '已禁止所有听讲书写';
                Message.success(desc);
            }, 1000)
        },
        /**
         * @name: 讲师开始加载白板
         * @msg: 
         * @return {*}
         */        
        async teacherOpenBoard(isWrite) {
            if (this.roomToken) return;
            this.isAllWrite = isWrite;
            this.isOpenWhiteboard = true;
            this.joinRoom();
        },
        /**
         * @name: 讲师关闭白板
         * @msg: 
         * @return {*}
         */        
        async teacherCloseBoard() {
            if (!this.roomToken) return;
            this.isAllWrite = false
            this.isOpenWhiteboard = false
            this.roomToken = null;
        },
        
    }
}
export default minix
